/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nhncorp.neptune.fs;

import java.io.IOException;

import com.nhncorp.neptune.common.LockServiceFactory;
import com.nhncorp.neptune.common.conf.NConfiguration;
import com.nhncorp.neptune.tabletserver.ChangeLog;
import com.nhncorp.neptune.tabletserver.TabletServerIF;
import com.nhncorp.pleiades.client.LockService;

public abstract class ChangeLogFileSystem implements ChangeLogFileSystemIF {
  private static ThreadLocal<ChangeLogFileSystemIF> threadLocal = new ThreadLocal<ChangeLogFileSystemIF>();
  
  public static synchronized ChangeLogFileSystemIF getChangeLogFileSystem(
                              NConfiguration conf,
                              TabletServerIF tabletServer, 
                              LockService lockService) throws IOException {
    boolean testMode = conf.getBoolean("testmode", false);
    
    ChangeLogFileSystemIF fs = threadLocal.get();
    if(fs != null) {
      return fs;
    }
    
    String fileSystem = conf.get("neptune.changelog.filesystem", "local");
    if("local".equals(fileSystem)) {
      fs = new LocalChangeLogFileSystem(conf);
    } else if (fileSystem.indexOf("pipe") >= 0) {
        boolean consistencyCheck = "true".equalsIgnoreCase(
            conf.get("changelog.pipe.consistency.check", "false"));
  	  fs = new PipeBasedChangeLogFileSystem(consistencyCheck);
  	  fs.init(fs, conf, tabletServer, lockService);
    } else {
      //fs = new LocalChangeLogFileSystem();
      System.out.println("FATAL: can't get ChangeLogFileSystem: " + fileSystem);
      System.exit(0);
    }
    threadLocal.set(fs);
    return fs;
  }
  
//  public void deleteAll() throws IOException {
//    String[] files = list();
//    if(files == null || files.length == 0) {
//      return;
//    }
//    
//    for(int i = 0; i < files.length; i++) {
//      delete(files[i]);
//    }
//  }
  
  public static void main(String[] args) throws Exception {
    NConfiguration conf = new NConfiguration();
    LockService lockService = 
      LockServiceFactory.getLockService(new NConfiguration(), "ChangeLogConsole_" + System.currentTimeMillis(), false);
    //lockService.readDir(Constants.SCHEMA);
    try {
      ChangeLogFileSystemIF fs = ChangeLogFileSystem.getChangeLogFileSystem(conf, null, lockService);
      
      fs.open(args[0], false);
      
      ChangeLog changeLog = null;
      while( (changeLog=fs.nextChangeLog(args[0])) != null) {
        System.out.println(changeLog);
      }
    } finally {
      lockService.close();
    }
  }
}
